package net.zjitc.boot2301demo.controller;


import net.zjitc.boot2301demo.pojo.Admin;
import net.zjitc.boot2301demo.service.AdminService;
import net.zjitc.boot2301demo.utils.APIResponse;
import net.zjitc.boot2301demo.utils.JwtUtil;
import net.zjitc.boot2301demo.utils.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;


    @PostMapping("login")
    @ResponseBody
//    @CrossOrigin("http://localhost:8888")
    public APIResponse<Token> login (@RequestBody Admin admin){
        JwtUtil jwtUtil=new JwtUtil();
        String token=jwtUtil.generateToken(admin.getUsername());
        //查询当前用户是否存在
        Admin user = adminService.findAdmin(admin.getUsername(),admin.getPassword());
        APIResponse apiResponse =new APIResponse<Token>();
        apiResponse.setData(new Token(""));
        if (user!=null){
            apiResponse.setErrno(0);
            apiResponse.setData(new Token(token));
            apiResponse.setErrmsg("登录成功");
        }else{
            apiResponse.setErrno(1000);
            apiResponse.setErrmsg("登录失败");
        }
        System.out.println(apiResponse);
        return apiResponse;
    }
}

